package com.hns.can.mainte.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.constants.GlobalConst;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.common.formula.FillParamsVo;
import com.hns.can.mainte.entity.AppCarMaintenInfoEntity;
import com.hns.can.mainte.vo.MaintenanceParamVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.UtilString;

/**
 * 车辆维保明细
 * @author:yecb
 * @email:lps00@163.com
 * @version Revision 2.0.0
 */
@Service
public class ReportCarMainteInfoService extends CommonService {
    
    @Autowired
    private FillCodeDscValue codeDscValue;

   /**
   * 维保信息的录入
   * @param entity 维保数据实体
   * @param request
   * @author:yecb
 * @throws Exception 
   * @email:lps00@163.com
   */
    @Transactional
    public void addMaintenanceInfo(AppCarMaintenInfoEntity entity,SysUserEntity user) throws Exception {
    	// 后台校验
    	checkAddAndUpdate(entity);
        String id = user.getId();
    	entity.setMaintenId(UtilString.getUUID());
        entity.setCorpId(user.getCorpId());
        entity.setOrganId(user.getOrganId());
        entity.setCrtTime(new Date());
        entity.setCrtUser(id);
        entity.setUpdateTime(new Date());
        entity.setUpdateUser(id);
        commonEntityDao.save(entity);
    }

    /**
     * 维保信息维护 - 查询维保信息 page
     * @param conditionVo
     * @return
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @Transactional
    public Pagination searchMaintenancePage(MaintenanceParamVo conditionVo) throws Exception {
    	//查询全部
    	if("All".equals(conditionVo.getMaintenCgy())){
    		conditionVo.setMaintenCgy("");
    	}
        Pagination page = this.myBatisPage("reportCarMainteInfo.getMaintenInfoCnt", "reportCarMainteInfo.getMaintenInfoList", conditionVo, AppCarMaintenInfoEntity.class);
        List list =  page.getData();
        Map<String,String> map = new HashMap<String,String>();
        map.put("licPltNo",  GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo vo = new FillParamsVo("carId", map);
        codeDscValue.fillInfoValue(list,new String[] {GlobalConst.CACHE_KEY_BASE_CAR_INFO}, vo);
        map = new HashMap<String,String>();
        map.put("maintenCgy", "MAIN1000");
	    codeDscValue.fillCodeDscValue(list, map);
        page.setData(list);
        return page;
    }
    
   /**
    * 维保信息维护 - 删除单条维保信息
    * @param maintenId
    * @author:yecb
    * @email:lps00@163.com
    */
    @Transactional
    public void delMaintenanceInfo(String maintenId) {
        AppCarMaintenInfoEntity entity = this.commonEntityDao.find(maintenId, AppCarMaintenInfoEntity.class);
        this.commonEntityDao.delete(entity);
    }

    /**
     * 维保信息维护 - 根据id获取维保信息
     * @param maintenId
     * @param request
     * @return
     * @author:yecb
     * @email:lps00@163.com
     */
    @Transactional
    public AppCarMaintenInfoEntity getMaintenanceInfoByMaintenId(String maintenId) {
        AppCarMaintenInfoEntity entity = this.commonEntityDao.find(maintenId, AppCarMaintenInfoEntity.class);
        return entity;
    }

    /**
     * 维保信息维护 - 修改维保信息
     * @param entity
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @Transactional
    public void uptMaintenanceInfo(AppCarMaintenInfoEntity entity) throws Exception {
        AppCarMaintenInfoEntity tmpEntity = this.commonEntityDao.find(entity.getMaintenId(), AppCarMaintenInfoEntity.class);
        PubUtil.copyPropertiesInclude(tmpEntity, entity, new String[]{"corpId","organId","carId","crtTime","crtUser"});
        this.commonEntityDao.update(entity);
    }

    /**
     * 维保信息维护 - 批量删除维保信息
     * @param maintenIds
     * @author:yecb
     * @email:lps00@163.com
     */
    @Transactional
    public void delMaintenanceInfos(List<String> maintenIds) {
    	String[] maintenArry=(String[]) maintenIds.toArray();
    	commonEntityDao.delete(AppCarMaintenInfoEntity.class, maintenArry);
    }
    
    /**
     * 检查添加和更新是否符合条件，后台校验
     * @param entity
     * @author:yecb
     * @email:lps00@163.com
     */
    private void checkAddAndUpdate(AppCarMaintenInfoEntity entity) throws Exception{
    	 List<AppCarMaintenInfoEntity> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("reportCarMainteInfo.getCarMainteInfo", entity), AppCarMaintenInfoEntity.class);
    	 if(PubUtil.isNotEmpty(list)&&list.size()>0){
    		 throw new BusinessException(MsgType.WARN.getFlag(),"存在重复的数据，不允许添加！");
    	 }
    }
    
}
